package com.itgacl.magic4j.libcommon.component.ws.cluster.mq;

import com.itgacl.magic4j.libcommon.component.ws.cluster.Constants;
import com.itgacl.magic4j.libcommon.component.ws.cluster.listener.WebsocketMsgListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;


/**
 * 广播消息模式
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@ConditionalOnExpression("${ws.enable} == true && '${ws.cluster.type}'.equals('mq')")
@Slf4j
@Component
@RocketMQMessageListener(topic = Constants.TOPIC.WEBSOCKET_MSG_NOTIFY,selectorExpression="*",consumerGroup = "websocketPush_group",messageModel = MessageModel.BROADCASTING)
public class RocketMqBroadcastMsgListener implements RocketMQListener<MessageExt> {

    @Autowired
    private WebsocketMsgListener websocketMsgListener;

    /**
     *
     * 接收消息
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String msg = new String(body);
        log.debug("websocket-push 消费到RocketMq发送的消息：{}", msg);
        websocketMsgListener.receiveMessage(msg);
    }
}
